<?php
/**
 * Author: DefinitlyEvil
 * Created at: 2020/4/14 19:55
 */

namespace App\PublicAPI;


use App\Controller;
use App\Tools\TransactionTools;

class UserController extends Controller
{
    public function current($params = []) {
        return [$this->user, true];
    }

    public function balance($params = []) {
        if(!isset($params['t']) or !in_array($params['t'], TransactionTools::TYPES)) throw new \Exception('invalid type');
        return [['balance' => TransactionTools::getBalance($this->getPdo(), $this->user['id'], $params['t'])], true];
    }

    public function alive($params = []) {
        $room_id = false;
        if(isset($params['r']) and is_int($params['r'])) {
            $room_id = intval($params['r']);
        }

        $time = time();

        $pdo = $this->getPdo();

        if($room_id !== false) {
            $need_check_room = true;

            // check join
            /*$stm = $pdo->prepare('SELECT * FROM `account_online` WHERE `account_id`=:acc AND `last_seen`>(:t-60)');
            $stm->bindParam(':acc', $this->user['id'], \PDO::PARAM_INT);
            $stm->bindParam(':t', $time, \PDO::PARAM_INT);
            if ($stm->execute() === false) return ['err', false];
            $last_record = $stm->fetch(\PDO::FETCH_ASSOC);
            if (isset($last_record['room_id']) and is_int($last_record['room_id'])) {
                $last_room_id = intval($last_record['room_id']);
                if ($last_room_id === $room_id) {
                    // switch room
                    $need_check_room = false;
                }
            }*/

            if ($need_check_room) {
                $stm = $pdo->prepare('SELECT * FROM `rooms` WHERE `id`=:r AND `expire_time`>:t AND `can_join`=1');
                $stm->bindParam(':r', $room_id, \PDO::PARAM_INT);
                $stm->bindParam(':t', $time, \PDO::PARAM_INT);
                if ($stm->execute() === false) return ['err room check', false];
                if ($stm->fetch(\PDO::FETCH_ASSOC) === false) return ['can not join room! ', false];
            }
        }

        $stm = $pdo->prepare("INSERT INTO `account_online` (`account_id`,`last_seen`,`room_id`) VALUES(:acc,:t,:r) ON DUPLICATE KEY UPDATE `last_seen`=VALUES(`last_seen`), `room_id`=VALUES(`room_id`)");
        $stm->bindParam(':acc', $this->user['id'], \PDO::PARAM_INT);
        $stm->bindParam(':t', $time, \PDO::PARAM_INT);
        if($room_id !== false) {
            $stm->bindParam(':r', $room_id, \PDO::PARAM_INT);
        } else {
            $stm->bindValue(':r', null);
        }
        $stm->execute();

        return true;
    }

    public function online() {
        $time = time();

        $pdo = $this->getPdo();
        $stm = $pdo->prepare(<<< EOL
SELECT
  `accounts`.`id`,
  `accounts`.`username`,
  `accounts`.`uuid`
FROM 
  `account_online`
LEFT JOIN `accounts` ON `accounts`.`id`=`account_online`.`account_id`
WHERE `account_online`.`last_seen`>=(:t - 300)
LIMIT 100
EOL
);
        $stm->bindParam(':t', $time, \PDO::PARAM_INT);
        $stm->execute();
        $r = $stm->fetchAll(\PDO::FETCH_ASSOC);
        if($r === false) return ['db error', false];
        return [['online' => $r], true];
    }

}